<?php

namespace app\admin\service;

use support\spark\exception\ApiException;
use support\spark\model\system\CrontabTask;
use support\spark\model\system\CrontabTaskLog;
use think\Validate;

class TaskService
{


    public function getList($param)
    {
        $limit = isset($param['limit']) ? $param['limit'] : 10;
        $taskModel = new CrontabTask();
        $where = [];
        $list = $taskModel->getPaginateList($where, ['*'], ['id' => 'desc']);
        return dataReturn(0, 'success', $list);
    }

    /**
     * 添加定时任务
     * @param $param
     * @return array
     */
    public function addTask($param)
    {
        $setExpressModel = new CrontabTask();
        $has = $setExpressModel->checkUnique([
            'title' => $param['title']
        ]);
        if (!empty($has)) {
            throw new  ApiException('定时任务名称已存在', -1);
        }
        $param['create_time'] = time();
        $param['update_time'] = time();
        $setExpressModel->insertOne($param);
        return dataReturn(0, '添加成功');
    }

    /**
     * 修改定时任务
     * @param $param
     * @return array
     */
    public function editTask($param)
    {
        $setExpressModel = new CrontabTask();
        $update[$param['field']] = $param['value'];
        $update['update_time'] = time();
        $setExpressModel->updateById($update, $param['id']);
        return dataReturn(0, '修改成功');
    }

    public function getFlowList($param)
    {
        $limit = isset($param['limit']) ? $param['limit'] : 10;
        $taskModel = new CrontabTaskLog();
        $where = ['sid' => $param['sid']];
        $list=$taskModel->getPaginateList($where, ['*'], ['id' => 'desc']);
        return dataReturn(0, 'success', $list);
    }


}